1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!-- Very Complex Result Map -->  
<resultMap id="detailedBlogResultMap" type="Blog">
<constructor>
<idArg column="blog_id" javaType="int"/>
</constructor>
<result property="title" column="blog_title"/>
<association property="author" column="blog_author_id" javaType=" Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
<result property="password" column="author_password"/>
<result property="email" column="author_email"/>
<result property="bio" column="author_bio"/>
<result property="favouriteSection" column="author_favourite_section"/>
</association>
<collection property="posts" ofType="Post">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<association property="author" column="post_author_id" javaType="Author"/>
<collection property="comments" column="post_id" ofType=" Comment">
<id property="id" column="comment_id"/>
</collection>
<collection property="tags" column="post_id" ofType=" Tag" >
<id property="id" column="tag_id"/>
</collection>
<discriminator javaType="int" column="draft">
<case value="1" resultType="DraftPost"/>
</discriminator>
</collection>
</resultMap> <wbr>

resultmap

  • constructor 实例化的时候通过构造器将结果集注入类中
  • ID 结果集ID 方便全局调用
  • result 注入一个字段或者Javabean属性的结果。
  • association 一对一嵌套
  • collection 一对多嵌套
  • discriminator 使用一个结果值以决定使用哪个resultMap

Id result 元素

1
2
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>

Association 元素

1
2
3
4
<association property="author" column="blog_author_id" javaType=" Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
</association>

association 元素处理一对一类型关系 这时需要告诉mybatis 如何加载一个联合查询,mybatis使用两种方式加载1 通过执行另一个返回预期复杂类型的映射SQL语句2.通过嵌套结果映射来处理连接结果集的重复子集。

1
2
3
4
5
6
7
8
1<resultMap id=”blogResult” type=”Blog”>  
<association property="author" column="blog_author_id" javaType="Author"
select=”selectAuthor”/>
</resultMap>

<select id=”selectAuthor” parameterType=”int” resultType="Author">
SELECT * FROM AUTHOR WHERE ID = #{id}
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<resultMap id="blogResult" type="Blog">  
<id property=”blog_id” column="id" />
<result property="title" column="blog_title"/>
<association property="author" column="blog_author_id" javaType="Author"
resultMap=”authorResult”/>
</resultMap>

<resultMap id="authorResult" type="Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
<result property="password" column="author_password"/>
<result property="email" column="author_email"/>
<result property="bio" column="author_bio"/>
</resultMap>

也可以直接把id 标签 result 标签写到里面。

collection 元素

1
2
3
4
5
<collection property="posts" ofType="domain.blog.Post">  
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<result property="body" column="post_body"/>
</collection>
1
2
3
4
5
6
7
8
9
<resultMap id=”blogResult” type=”Blog”>  
<collection property="posts" javaType=”ArrayList” column="blog_id" ofType="Post" select=”selectPostsForBlog”/>
</resultMap>
<select id=”selectBlog” parameterType=”int” resultMap=”blogResult”>
SELECT * FROM BLOG WHERE ID = #{id}
</select>
<select id=”selectPostsForBlog” parameterType=”int” resultType="Author">
SELECT * FROM POST WHERE BLOG_ID = #{id}
</select>

这时会有一个新的属性oftype 这个元素是用来区别JavaBean属性类型和集合类型所包括的类型。